<?php
	error_reporting(0);
	session_start();
	if(!session_is_registered(myusername)){
		header("location:main_login.php");
	}
	$dbconn = pg_connect ("host=localhost port=5432 dbname=postgres user=postgres password=leliput93");
	if (!$dbconn) {
		echo "An error occured.\n";
		exit;
	}

	$sign=false;
	extract($_POST);
	if ($submit) {
		$sign=true;
		$date1=strtotime("$year1-$month1-$day1");
		$date2=strtotime("$year2-$month2-$day2");

		if($date1>$date2){
			$err="<br/><label style=color:#FF0000> Input tanggal pada \"Periode Transaksi\" tidak valid.\n
         Periode \"sampai\" tidak boleh lebih dahulu dibandingkan periode \"dari\". </label>";
			$sign=false;
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<title>Supermarket</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="style.css" type="text/css" />
		<script type="text/javascript" src = "tgl.js"></script>
	</head>

	<body>

		<div id="container" class="clearfix">
			<h1>HEMART B10<em> CABANG <?php echo " $_SESSION[namacabang]";?></em></h1>
			<h2></h2>

			 <div id="content">
				<?php  if ($sign==false) { ?>
					<h3>Rekapitulasi Transaksi Pembelian</h3>
					<br/><br/>

					<b>Pilihlah kriteria untuk menampilkan rekapitulasi transaksi pembelian:</b><br/><br/>
					<form name="dft_transaksi" method = "post" action = "rekapitulasi1.php">

					<b>Cabang: </b><select name = "namacabang">
					<?php
					$result = pg_query($dbconn, "select nama from supermarket.cabang");
					while ( $row = pg_fetch_array($result))
							print("<option>".$row[0]."</option>");
					?>
					</select><br/><br/>

					<label><b>Periode transaksi :</b></label><br/>
					Dari
						<SCRIPT>fill_select_dft1(document.dft_transaksi); year_install_dft1(document.dft_transaksi);</script>
					sampai
						<SCRIPT>fill_select_dft2(document.dft_transaksi); year_install_dft2(document.dft_transaksi);</script>
					<?php echo "$err"; ?><br/><br/>
					<label><b>Satuan waktu rekapitulasi :</b></label><br/>
					<input type="radio" name="satuan" value="Harian"  checked /> Harian<br />
					<input type="radio" name="satuan" value="Mingguan" /> Mingguan<br/>
					<input type="radio" name="satuan" value="Bulanan" /> Bulanan
					<br/><br/>
					<input type="submit" name="submit" id="button" value="Submit" />
					<input type="reset" name="button" id="button" value="Reset" />
					</p>
					<br/><br/>
					</form>
				<?php }
				else {
					echo "<h3>Rekapitulasi Transaksi Pembelian</h3><br/>";
					$awal = $year1."-".$month1."-".$day1;
					$numy1 = (int)$year1;
					$numm1 = (int)$month1;
					$numd1 = (int)$day1;

					$akhir = $year2."-".$month2."-".$day2;
					$numy2 = (int)$year2;
					$numm2 = (int)$month2;
					$numd2 = (int)$day2;

					$nama_bulan=array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
					$namabln = array ("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
					$jumlah_hari = array("",31,28,31,30,31,30,31,31,30,31,30,31);
					$jumlah_hari2 = array("",31,29,31,30,31,30,31,31,30,31,30,31);

					$help = (($date2-$date1)/60/60/24);

					if ($satuan == "Harian") {
						$result = pg_query($dbconn, "select (tgl_transaksi - '$awal') as waktu, count(distinct kode_pembelian) as frekuensi, sum (harga_beli*kuantitas) as total_transaksi
											from (supermarket.pembelian pb natural join supermarket.pembelian_produk), supermarket.cabang cb
											where tgl_transaksi >= '$awal' and tgl_transaksi<='$akhir' and cb.nama='$namacabang' and cb.kode=pb.kode_cabang
											group by tgl_transaksi
											order by tgl_transaksi asc");

						$count_tgl = $numd1;
						$count_bln = $numm1;
						$count_thn = $numy1;
						$counter = $numd1-1;
						$bulan = array();
						$tahun = array();
						$frek = array();
						$total = array();
						$bulan[0] = $namabln[$numm1];
						$tahun[0] = $numy1;

						while ($row = pg_fetch_row($result)) {
							$frek[$row[0]] = $row[1];
							$total[$row[0]] = $row[2];
						}
						$count=0;
						for($j=0;$j<= $help;$j++) {
						$count+=$frek[$j];
						}

						echo "Pada periode ".$day1." ".$nama_bulan[$month1-1]." ".$year1." sampai ".$day2." ".$nama_bulan[$month2-1]." ".$year2." ada " . $count . " transaksi<br/>";
						echo "<br/><br/><b>Tabel Rekapitulasi Transaksi Harian</b><br/>";
						echo "<table>
							<th>Satuan Waktu Hari</th>
							<th>Frekuensi Transaksi</th>
							<th>Total Transaksi</th>";

						for($j=0;$j<= $help;$j++) {

							$counter++;
							$c=0;
							if(($count_thn%4==0)&&($count_bln==2)) {
								$jml_hr=$jumlah_hari[$count_bln]+1;
							}
							else {
								$jml_hr=$jumlah_hari[$count_bln];
							}
							if($counter>$jml_hr){
								$counter=1;
								$count_bln++;
								$c++;
								$bulan[$c]=$namabln[$count_bln];

								if($count_bln>12){
									$count_thn++;
									$count_bln=$count_bln%12;
								}

							}

							print("<tr>");
							echo "<td>$counter $namabln[$count_bln] $count_thn</td><td>$frek[$j]</td><td align = 'right'>$total[$j]</td>";
							print("</tr>");
						}
						echo "</table>";

					}


					else if ($satuan == "Mingguan") {
						$result = pg_query($dbconn, "select ((tgl_transaksi - '$awal') / integer '7') as waktu, count(distinct kode_pembelian) as frekuensi, sum (harga_beli*kuantitas) as total_transaksi
											from (supermarket.pembelian pb natural join supermarket.pembelian_produk), supermarket.cabang cb
											where tgl_transaksi >= '$awal' and tgl_transaksi<='$akhir' and cb.nama='$namacabang' and cb.kode=pb.kode_cabang
											group by waktu
											order by waktu asc");

						$week = floor($help/7);
						$frekweek = array();
						$totalweek = array();

						$countweek=0;
						while ($row = pg_fetch_row($result)) {
							$frekweek[$row[0]] = $row[1];
							$totalweek[$row[0]] = $row[2];
						}
						$count=0;
						for($j=0;$j<= $help;$j++) {
						$count+=$frekweek[$j];
						}

						echo "Pada periode ".$day1." ".$nama_bulan[$month1-1]." ".$year1." sampai ".$day2." ".$nama_bulan[$month2-1]." ".$year2." ada " . $count . " transaksi<br/>";

						echo "<br/><br/><b>Tabel Rekapitulasi Transaksi Mingguan</b><br/>";
						echo "<table>
						<th>Satuan Waktu Minggu</th>
						<th>Frekuensi Transaksi</th>
						<th>Total Transaksi</th>";


						$startweek=$numd1;
						$count_month_end = $numm1;
						$count_month_start= $numm1;
						$count_thn=$numy1;
						$count_thnawal = $numy1;

						while ($row = pg_fetch_row($result)) {
							$frekweek[$row[0]] = $row[1];
							$totalweek[$row[0]] = $row[2];
						}

						for($ii = 0; $ii <= $week; $ii++){
							if($count_thn%4==0) {
								if($ii==0){
									$endweek = $startweek + 6;

									if($endweek > $jumlah_hari2[$numm1]){
										$endweek = $endweek % $jumlah_hari2[$numm1] ;
										$count_month_end = $count_month_end+1;
									}
									if($count_month_end>12){
										$count_month_end = $count_month_end %12;
										$count_thn++;
									}
									print("<tr>");
									echo "<td>$startweek $namabln[$numm1] $count_thnawal - $endweek $namabln[$count_month_end] $count_thn </td>
									<td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
									$count_month_start = $count_month_end;
									$count_thnawal = $count_thn;

								}
								else{

									$startweek = $endweek+1;
									if($startweek>$jumlah_hari2[$count_month_end]){
										$startweek=1;
										$count_month_start++;
										$count_month_end++;
									}
									$endweek =  $startweek + 6;
									if($endweek > $jumlah_hari2[$count_month_end]){
										$endweek = $endweek % $jumlah_hari2[$count_month_end];
										$count_month_end = $count_month_end +1;

										if($count_month_end>12){
											$count_month_end = $count_month_end%12;
											$count_thn++;

										}
										print("<tr>");
										echo "<td>$startweek $namabln[$count_month_start] $count_thnawal - $endweek $namabln[$count_month_end]
										$count_thn</td><td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
										$count_month_start = $count_month_end;
										$count_thnawal = $count_thn;
									}
									else {
										echo "<tr><td>$startweek $namabln[$count_month_start] $count_thnawal - $endweek $namabln[$count_month_end]
										$count_thn</td><td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
									}

								}
							}
							else {
								if($ii==0){
									$endweek = $startweek + 6;

									if($endweek > $jumlah_hari[$numm1]){
										$endweek = $endweek % $jumlah_hari[$numm1] ;
										$count_month_end = $count_month_end+1;
									}
									if($count_month_end>12){
										$count_month_end = $count_month_end %12;
										$count_thn++;
									}
									print("<tr>");
									echo "<td>$startweek $namabln[$numm1] $count_thnawal - $endweek $namabln[$count_month_end] $count_thn </td>
									<td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
									$count_month_start = $count_month_end;
									$count_thnawal = $count_thn;

								}
								else{

									$startweek = $endweek +1;
									if($startweek>$jumlah_hari[$count_month_end]){
										$startweek=1;
										$count_month_start++;
										$count_month_end++;
									}
									$endweek =  $startweek + 6;

									if($endweek > $jumlah_hari[$count_month_end]){
										$endweek = $endweek % $jumlah_hari[$count_month_end];
										$count_month_end = $count_month_end+1;

										if($count_month_end>12){
											$count_month_end = $count_month_end%12;
											$count_thn++;

										}
										print("<tr>");
										echo "<td>$startweek $namabln[$count_month_start] $count_thnawal - $endweek $namabln[$count_month_end]
											$count_thn</td><td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
										$count_month_start = $count_month_end;
										$count_thnawal = $count_thn;
									}
									else {
										echo "<tr><td>$startweek $namabln[$count_month_start] $count_thnawal - $endweek $namabln[$count_month_end]
										$count_thn</td><td>$frekweek[$ii]</td><td align = 'right'>$totalweek[$ii]</td></tr>";
									}
								}
							}
						}

						echo "</table>";
					}

					else if ($satuan == "Bulanan") {
						$result = pg_query($dbconn, "select extract(month from pb.tgl_transaksi) as waktu, extract(year from pb.tgl_transaksi) as waktu1, count(distinct kode_pembelian) as frekuensi, sum (harga_beli*kuantitas) as total_transaksi
											from (supermarket.pembelian pb natural join supermarket.pembelian_produk), supermarket.cabang cb
											where tgl_transaksi >= '$awal' and tgl_transaksi<='$akhir' and cb.nama='$namacabang' and cb.kode=pb.kode_cabang
											group by waktu, waktu1
											order by waktu1 asc");

						$count = pg_num_rows($result);

						$counter=0;
						$total="";
						$frekuen="";
						$count_transaksi=0;

						if ($numd1!=1) {
							$help = (($date2-strtotime("$year1-$month1-1"))/60/60/24);
							$numd1=1;
						}


						$count_month=0;
						$print_tabel = "";
						$print_tabel .= "<table>
						<th>Satuan Waktu Bulan</th>
						<th>Frekuensi Transaksi</th>
						<th>Total Transaksi</th>";

						$month = floor($help/30);

						$count_day_start = $numd1;
						$count_day_end = $numd1;
						$count_month_end = $numm1;
						$count_month_start= $numm1;

						$count_thn = $numy1;
						$count_thnawal = $numy1;

						for($ii = 0; $ii <=$month; $ii++){
							if(($count_thn%4==0)) {
								if($ii==0){
									$count_day_end = $count_day_start +($jumlah_hari2[$numm1]-1);
									if($count_day_end > $jumlah_hari2[$numm1]){
										$count_day_end = $count_day_end % $jml_hr;
									}

									if($count_month_end > 12) {
										$count_month_end = $count_month_end % 12;
										$count_thn++;
									}

									if ($counter<$count) {
										$row=pg_fetch_array ($result,$counter);
										$x=(int)$row[0];
										$y=(int)$row[1];

										if ($x==$numm1&&$y==$count_thnawal) {
											$frekuen=$row[2];
											$total=$row[3];
											$counter++;
											$count_transaksi+=(int)$row[2];
										}

										else{
											$frekuen = "";
											$total = "";
											$count_transaksi+=0;
										}
									}

									else {
										$frekuen = "";
										$total = "";
										$count_transaksi+=0;
									}

									if ($count_month_end==$numm2&&$count_thn==$numy2) {
										$print_tabel .= "<tr><td>$day1 $namabln[$numm1] $count_thnawal - $day2 $namabln[$count_month_end] $count_thn</td>
										<td>$frekuen</td><td align = 'right'>$total</td></tr>";
										break;
									}
									else {
										$print_tabel .= "<tr><td>$day1 $namabln[$numm1] $count_thnawal - $count_day_end $namabln[$count_month_end] $count_thn</td>
											<td>$frekuen</td><td align = 'right'>$total</td></tr>";
									}

									$count_month_start = $count_month_end;
									$count_thnawal = $count_thn;

								}
								else {

									$count_day_start = $count_day_end +1;

									if($count_day_start > $jumlah_hari2[$count_month_end]){
										$count_day_start = $count_day_start % $jumlah_hari2[$count_month_end];
									}
									if($count_day_end > $jumlah_hari2[$count_month_end]){
										$count_day_end = $count_day_end % $jumlah_hari2[$count_month_end];
									}

									$count_month_end++;
									if($count_month_end > 12){
										$count_month_end = ($count_month_end % 12);
										$count_thn++;
										$count_thnawal =$count_thn;
									}
									$count_day_end = $count_day_start + ($jumlah_hari2[$count_month_end]-1);

									if ($counter<$count) {
										$row=pg_fetch_array ($result,$counter);
										$x=(int)$row[0];
										$y=(int)$row[1];

										if ($x==$count_month_end&&$y==$count_thnawal) {
											$frekuen=$row[2];
											$total=$row[3];
											$counter++;
											$count_transaksi+=(int)$row[2];
										}
										else{
											$frekuen = "";
											$total = "";
											$count_transaksi+=0;
										}
									}
									else {
										$frekuen = "";
										$total = "";
										$count_transaksi+=0;
									}
									if ($count_month_end==$numm2&&$count_thn==$numy2) {
										$print_tabel .= "<tr><td>$count_day_start $namabln[$count_month_end] $count_thnawal - $day2 $namabln[$count_month_end] $count_thn</td>
										<td>$frekuen</td><td align = 'right'>$total</td></tr>";
										break;
									}
									else {
										$print_tabel .= "<tr><td>$count_day_start $namabln[$count_month_end] $count_thnawal - $count_day_end $namabln[$count_month_end] $count_thn</td>
											<td>$frekuen</td><td align = 'right'>$total</td></tr>";
											echo $frekuen . "=".$total;
									}

									$count_thnawal=$count_thn;
								}
							}
							else {
								if($ii==0){
									$count_day_end = $count_day_start +($jumlah_hari[$numm1]-1);
									if($count_day_end > $jumlah_hari[$numm1]){
										$count_day_end = $count_day_end % $jml_hr;
									}
									if($count_month_end > 12) {
										$count_month_end = $count_month_end % 12;
										$count_thn++;
									}


									if ($counter<$count) {
										$row=pg_fetch_array ($result,$counter);
										$x=(int)$row[0];
										$y=(int)$row[1];

										if ($x==$numm1 && $y==$count_thnawal) {
											$frekuen=$row[2];
											$total=$row[3];
											$counter++;
											$count_transaksi+=(int)$row[2];
										}
										else{
											$frekuen = "";
											$total = "";
											$count_transaksi+=0;
										}
									}
									else {
										$frekuen = "";
										$total = "";
										$count_transaksi+=0;
									}
									if ($count_month_end==$numm2&&$count_thn==$numy2) {
										$print_tabel .= "<tr><td>$day1 $namabln[$numm1] $count_thnawal - $day2 $namabln[$count_month_end] $count_thn</td>
										<td>$frekuen</td><td align = 'right'>$total</td></tr>";
										break;
									}
									else {
										$print_tabel .= "<tr><td>$day1 $namabln[$numm1] $count_thnawal - $count_day_end $namabln[$count_month_end] $count_thn</td>
										<td>$frekuen</td><td align = 'right'>$total</td></tr>";
									}
									$count_month_start = $count_month_end;
									$count_thnawal = $count_thn;

								}
								else {

									$count_day_start = $count_day_end +1;
									if($count_day_start > $jumlah_hari[$count_month_end]){
										$count_day_start = $count_day_start % $jumlah_hari[$count_month_end];
									}
									if($count_day_end > $jumlah_hari[$count_month_end]){
										$count_day_end = $count_day_end % $jumlah_hari[$count_month_end];

									}

									$count_month_end++;
									if($count_month_end > 12){
										$count_month_end = ($count_month_end % 12);
										$count_thn++;
										$count_thnawal =$count_thn;
									}

									$count_day_end = $count_day_start + ($jumlah_hari[$count_month_end]-1);

									if ($counter<$count) {
										$row=pg_fetch_array ($result,$counter);
										$x=(int)$row[0];
										$y=(int)$row[1];

										if ($x==$count_month_end && $y==$count_thnawal) {
											$frekuen=$row[2];
											$total=$row[3];
											$counter++;
											$count_transaksi+=(int)$row[2];
										}
										else{
											$frekuen = "";
											$total = "";
											$count_transaksi+=0;
										}
									}
									else {
										$frekuen = "";
										$total = "";
										$count_transaksi+=0;
									}
									if ($count_month_end==$numm2&&$count_thn==$numy2) {
										$print_tabel .= "<tr><td>$count_day_start $namabln[$count_month_end] $count_thnawal - $day2 $namabln[$count_month_end] $count_thn</td>
										<td>$frekuen</td><td align = 'right'>$total</td></tr>";
										break;
									}
									else {
										$print_tabel .= "<tr><td>$count_day_start $namabln[$count_month_end] $count_thnawal - $count_day_end $namabln[$count_month_end] $count_thn</td>
											<td>$frekuen</td><td align = 'right'>$total</td></tr>";
									}
									$count_thnawal=$count_thn;
								}
							}

						}

						echo "Pada periode ".$day1." ".$nama_bulan[$month1-1]." ".$year1." sampai ".$day2." ".$nama_bulan[$month2-1]." ".$year2." ada " . $count_transaksi . " transaksi<br/><br/>";
            echo "<br/><br/><b>Tabel Rekapitulasi Transaksi Bulanan</b><br/>";
						echo $print_tabel;
						echo "</table>";
					}
				}
				?>
			</div>

			<ul id="nav">
				<li><a href="login_success.php">Home</a></li>
				<?php

					if ($_SESSION['jabatan'] == 'Admin')
					echo "<li><a href='form_akun.php'>Buat akun</a></li>";
				?>
				<li><a href="q1.php">List Daftar Cabang</a></li>
				<?php
					if ($_SESSION['jabatan'] == 'Manajer Operasional') {
						echo "<li><a href='pencatatan1.php'>Pencatatan Transaksi Pembelian</a></li>";
					}
				?>
				<li><a href="daftar_transaksi1.php">Daftar Transaksi Pemasok</a></li>
				<li><a href="rekapitulasi1.php">Rekapitulasi Transaksi Pembelian</a></li>
				<li><a href="produk_terlaku.php" >Daftar Produk Terlaku</a></li>
				<li><a href="B10.php" title="View B10 info">About us</a></li>
				<li><a href="logout.php">Logout</a></li>
			</ul>

		</div>

		<address>
			&copy; 2008 All Rights Reserved. Designed by <a href="http://www.caddoo.net" title="Portfolio of Matthew Caddoo">Matthew Caddoo</a> for <a href="http://www.zymic.com" title="Zymic Free Templates">Zymic</a> <a href="http://www.zymic.com/free-templates">Free Templates</a>. <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a>.
		</address>

	</body>
</html>
